<?php
// +----------------------------------------------------------------------
// | Author: ZYN Fl
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;
class Goods extends Common
{
    public function index()
    {   
        $gtitle=input('gtitle');
        if($gtitle){
            $goods=Db::table('xcx_goods')->alias('a')->join('xcx_brand w','a.gbrand = w.bid','left')->join('xcx_category z','z.cid = a.gcate','left')->where("gtitle='$gtitle'")->order('gid desc')->paginate(8);
			$data=$goods->toArray();
			ifExist($data['data']);
		}else{
            $goods=Db::table('xcx_goods')->alias('a')->join('xcx_brand w','a.gbrand = w.bid','left')->join('xcx_category z','z.cid = a.gcate','left')->order('gid desc')->paginate(8);
        }
		
        $page = $goods->render();
		
        $this->assign("goods",$goods);
        $this->assign('a','Goods');
        $this->assign('page', $page);
        return $this->fetch();
    }
    public function addlist()
    {   
        $brand=Db::name('brand')->select();
		//print_R($brand);
        $category=Db::name("category")->select();
        $this->assign("brand",$brand);
        $this->assign("category",$category);
        $this->assign('a','Goods');
        return $this->fetch();
    }
    public function addxinx(){
        $gtitle = input('gtitle');
        $picAll = request()->file('pic_all');
       
        $file = request()->file('goodsthumb');
        $gbrand = input('gbrand');
        $gcate = input('gcate');
        $goodsnum = input('goodsnum');
        $glower = input('sprice/a');
        $glowerprice = $glower[0];
        $gfare = input('gfare');
        $sspec = input('sspec/a');
        $sprice = input('sprice/a');
        $snum = input('snum/a');
        // $filez = request()->file('goodsdetails');
        $goodstext = input('editorValue');
        // $imagesSrc=input('imagesSrc');
        $info = $file->move(ROOT_PATH .'public'. DS .'uploads');
        if($info)
        {
            $data['goodsthumb']=$info->getSaveName();
        }
       
        if($picAll)
        {
                $goodshow='';
                foreach ($picAll as $picAlls) 
                {
                        $allPic=$picAlls->move(ROOT_PATH.'public'. DS .'uploads');
                        if ($allPic) 
                        {
                           $goodshow.=$allPic->getSaveName().'|';
                        } 
                }

                $data['goodshow']=$goodshow;
        }
        $data['gtitle']=$gtitle;
        $data['gbrand']=$gbrand;
        $data['gcate']=$gcate;
        $data['goodsnum']=$goodsnum;
        $data['glowerprice']=$glowerprice;
        $data['gfare']=$gfare;
        $data['goodstext']=$goodstext;
        $goods = Db::name('goods')->insertGetId($data);
        if($goods){
              $i = '';
                    for($i; $i<count($sspec); $i++ )
                    {
                        // var_dump((int)$i);
                            $spec = array(
                            'sgoods' => $goods,
                            'sspec' => $sspec[(int)$i],
                            'sprice' => $sprice[(int)$i],
                            'snum' => $snum[(int)$i]
                            );
                          $specs=Db::name('specs')->insert($spec);
                          $this->redirect("Goods/index");
                    }
         }else{
            echo $file->getError();
        }
        
    }
    //编辑信息
    public function update()
    {   
        //品牌数据
        $brand=Db::name('brand')->select();
        //商品数据
        $gid = input('gid');
        $data=Db::name('goods')->where("gid='$gid'")->find();
        
        $goodshow=explode("|",rtrim($data['goodshow'],'|'));


       
        //规格数据
        $specs=Db::name('specs')->where("sgoods='$gid'")->select();
        //分类数据
        $category=Db::name('category')->select();
        $this->assign('a','Goods');
        $this->assign("data",$data);
        $this->assign('goodshow',$goodshow);
        $this->assign('brand',$brand);
        $this->assign('specs',$specs);
        $this->assign('category',$category);
        return $this->fetch();
    }
   

        public function saveGoods()
        {

            
            $gid = input('gid');
            $sid = input('sid/a');
            $gtitle = input('gtitle');
            $pic_oldlist = input('pic_oldlist');
            $goodsthumb = input('goodsthumb');
            $picAll = request()->file('pic_all');
            $file = request()->file('goodsthumb');
            $gbrand = input('gbrand');
            $gcate = input('gcate');
            $goodsnum = input('goodsnum');
            $glower = input('sprice/a');
            $glowerprice = $glower[0];
            $gfare = input('gfare');
            $sspec = input('sspec/a');
            $sprice = input('sprice/a');
            $snum = input('snum/a');
            $sgoods = input('sgoods');
            /*$goodsdetails = input('goodsdetails');
            $filed = request()->file('goodsdetails');*/
            $goodstext = input('editorValue');
            /*$imagesSrc=input('imagesSrc');
            if($imagesSrc)
            {
                $data['goodsdetails']=$imagesSrc;
            }*/
           
             if($picAll)
            {
                    $goodshow='';
                    foreach ($picAll as $picAlls) 
                    {
                            $allPic=$picAlls->move(ROOT_PATH.'public'. DS .'uploads');
                            if ($allPic) 
                            {
                               $goodshow.=$allPic->getSaveName().'|';
                            } 
                    }

                    $data['goodshow']=$pic_oldlist.$goodshow;
            }else{
                $data['goodshow']=$pic_oldlist;
            }

            if(request()->file('goodsthumb'))
            {
                $file=request()->file('goodsthumb');

                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');

                $data['goodsthumb']=$info->getSaveName();
            }
             $data['gtitle']=$gtitle;
             $data['gbrand']=$gbrand;
             $data['gcate']=$gcate;
             $data['goodsnum']=$goodsnum;
             $data['gfare']=$gfare;
             $data['glowerprice']=$glowerprice;
             $data['goodstext']=$goodstext;
         
            $goods = Db::name('goods')->where("gid='$gid'")->update($data);
            //var_dump($sid);exit;
                for($i=0; $i<count($sspec); $i++ ){
                    $spec = array(
                    // 'sid' => $sid[(int)$i],
                    'sgoods' => $gid,
                    'sspec' => $sspec[(int)$i],
                    'sprice' => $sprice[(int)$i],
                    'snum' => $snum[(int)$i]
                    );
                    if($sid[(int)$i]==0){
                    Db::name('specs')->insert($spec);
                    }else{
                    Db::name('specs')->where("sid=".$sid[(int)$i])->update($spec);
                    }
                }
               $this->redirect("Goods/index");
        }

public function test()
{
    return $this->fetch();
}

public function zrUploads(){

        $date=date("Ymd",time());

        $uploaddir = 'uploads/'.$date.'/';
        if (! file_exists ( $uploaddir )) 
        {

                mkdir ( "$uploaddir", 0777, true );
        }

        $uploadfile = $uploaddir. $_FILES['file']['name'];
     
        $newname=md5(time().rand(10000,99999)).".jpg";
        if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $newname)) {
            $file=$date."/".$newname;
            return  $file;
        }
   
    }
    //删除
    public function del(){
        $gid = input('gid');
        $data = array(
            'gid' =>$gid
            );
        $del=Db::name('goods')->where("gid='$gid'")->delete($data);
        if($del){
            return 1;
        }
    }
    //删除规格
    public function delspecs(){
        $sid = input('sid');
        $data = array(
            'sid' => $sid
            );
         $del=Db::name('specs')->where("sid='$sid'")->delete($data);
         if($del){
            return 1;
        }
    }
    //添加规格
    public function addguige(){
        $this->assign('a','Goods');
         return $this->fetch();
    }
}